package com.eqdd.yiqidian.service;

import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.IBinder;
import android.support.v7.app.AlertDialog;

import com.eqdd.yiqidian.base.BaseApplication;
import com.eqdd.yiqidian.base.CodeRule;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.User;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonCallBack;
import com.eqdd.yiqidian.rxbus2.RxBus;
import com.eqdd.yiqidian.ui.LoginActivity;
import com.eqdd.yiqidian.ui.mine.setting.XiTongSettingActivity;
import com.eqdd.yiqidian.utils.EncryptUtil;
import com.eqdd.yiqidian.utils.GreenDaoUtil;
import com.eqdd.yiqidian.utils.LogoutUtil;
import com.eqdd.yiqidian.utils.SPUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ChooseAlertView;
import com.eqdd.yiqidian.widget.EditDialogFragment;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.HashMap;
import java.util.List;

import io.rong.imkit.RongIM;
import io.rong.imlib.model.UserInfo;
import rx.schedulers.Schedulers;

public class UserService extends Service {
    public UserService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Schedulers.newThread().createWorker().schedule(() -> {
            User user = GreenDaoUtil.getUser();
            if (user != null) {
                HashMap<String, String> map = new HashMap<>();
                map.put("u1", user.getUname());
                map.put("u2", EncryptUtil.sha1(SPUtil.getParam(Config.PASS, "") + "EQD"));
                OkGo.<HttpResult<User>>post(HttpConfig.BASE_URL + HttpConfig.LOGIN)
                        .params(map)
                        .execute(new JsonCallBack<HttpResult<User>>() {
                            @Override
                            public void onSuccess(Response<HttpResult<User>> response) {
                                HttpResult<User> httpResult = response.body();
                                if (httpResult.getStatus() == 200) {
                                    GreenDaoUtil.insert(httpResult.getItems());
                                    RxBus.getDefault().send(CodeRule.USER_INFO);
                                    RongIM.getInstance().setCurrentUserInfo(new UserInfo(httpResult.getItems().getGuid(), httpResult.getItems().getPname(), Uri.parse(httpResult.getItems().getIphoto())));
                                    RongIM.getInstance().refreshUserInfoCache(new UserInfo(httpResult.getItems().getGuid(), httpResult.getItems().getPname(), Uri.parse(httpResult.getItems().getIphoto())));
                                } else {
                                    Activity activity;
                                    if (BaseApplication.context instanceof Activity) {
                                        activity = (Activity) BaseApplication.context;
                                    } else {
                                        return;
                                    }
                                    AlertDialog alertDialog = ChooseAlertView.getAlertDialog(activity, "提醒", "身份校验失败,请重新登陆!", new EditDialogFragment.OnClickListener() {
                                        @Override
                                        public void onClick(List<String> contents) {
                                            onUnbind(intent);
                                            GreenDaoUtil.deleteAllUser();
                                            Intent intent1 = new Intent(activity, LoginActivity.class);
                                            activity.startActivity(intent1);
                                            activity.finish();
                                        }
                                    });
                                    alertDialog.show();
                                }
                            }


                        });
            }

        });
        return super.onStartCommand(intent, flags, startId);
    }
}
